<?php

namespace app\modules\admin\models;

use Yii;

/**
 * This is the model class for table "admin_access".
 *
 * @property int $id 权限id
 * @property string $pid
 * @property string $access_sn
 * @property string $access_name 访问表
 * @property string $module
 * @property string $controller
 * @property string $action
 * @property int $level 1:一级菜单 2:二级菜单 3:三级菜单(按钮)
 * @property int $sort 排序值
 * @property int $deleted 是否删除
 * @property string $created 创建时间
 * @property string $modified 修改时间
 */
class AdminAccess extends BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'admin_access';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['pid', 'access_sn', 'access_name'], 'required'],
            [['pid'], 'string'],
            [['level', 'sort', 'deleted'], 'integer'],
            [['created', 'modified'], 'safe'],
            [['access_sn'], 'string', 'max' => 50],
            [['access_name', 'module', 'controller', 'action'], 'string', 'max' => 45],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', '权限id'),
            'pid' => Yii::t('app', 'Pid'),
            'access_sn' => Yii::t('app', 'Access Sn'),
            'access_name' => Yii::t('app', '访问表'),
            'module' => Yii::t('app', 'Module'),
            'controller' => Yii::t('app', 'Controller'),
            'action' => Yii::t('app', 'Action'),
            'level' => Yii::t('app', '1:一级菜单 2:二级菜单 3:三级菜单(按钮)'),
            'sort' => Yii::t('app', '排序值'),
            'deleted' => Yii::t('app', '是否删除'),
            'created' => Yii::t('app', '创建时间'),
            'modified' => Yii::t('app', '修改时间'),
        ];
    }


    /**
     * 获取权限列表
     * @param type $field
     * @return type
     */
    public function getAccessList($offset, $limit, $isCount = FALSE, $field = array('*'))
    {
        $obj = $this->orm();
        if ($isCount) {
            $result = $obj->count();
        } else {
            $result = $obj->select($field)
                ->limit($limit)
                ->offset($offset)
                ->asArray()
                ->all();
        }
        return $result;
    }

    /**
     * 获取权限
     * @param type $id
     * @return type
     */
    public function getAccessById($id)
    {
        return $this->orm()
            ->andWhere(array('access_id' => $id))
            ->asArray()
            ->find();
    }

    /**
     * 添加文章
     *
     * @access public
     * @author furong
     * @param $data
     * @return bool
     * @since  2017年4月10日 09:59:33
     * @abstract
     */
    public function addAccess($data)
    {
        if (empty($data) || empty($data['access_sn'])) {
            return false;
        }
        $data['modified'] = getDateTime();
        $model = $this->orm();
        $count = $model->andWhere(['access_sn' => $data['access_sn']])
            ->count();
        if (!$count) {
            #添加
            unset($data[self::$this->primaryKey[0]]);
            $data['created'] = $data['modified'];
            $this->setAttributes($data);
            $result = $this->insert(false);
            $id = \Yii::$app->db->getLastInsertID();
        } else {
            #修改
            $model = $model->andWhere(['access_sn' => $data['access_sn']])
                ->asArray()->one();
            $model->setAttributes($data);
            $result = $model->update();
            $id = self::$this->primaryKey[0];
            $id = $model->$id;
        }
        return $result ? $id : $result;
    }
}
